#!/bin/bash

# Revert changes made in avahi-0.6.25.ebuild CVS revision 1.4. See also
# http://sources.gentoo.org/net-dns/avahi/avahi-0.6.25.ebuild?r1=1.3&r2=1.4

set -e
cd "$(dirname "$0")"/../..

# Clone avahi dir from main portage tree
rm -r net-dns/avahi
cp -r "${PORTDIR:-/usr/portage}/net-dns/avahi" net-dns/

# Make adjustments to ebuilds
sed -i \
    -e '/^IUSE="[^"]*$/{N}' \
    -e 's/^\(IUSE=.*\) qt4/\1 qt3 qt4/' \
    -e 's/--disable-qt3/$(use_enable qt3)/' \
    -e 's/^\([ \t]*\)\(qt4? ( x11-libs\/qt-core:4\)/\1qt3? ( x11-libs\/qt-meta:3 )\n\1\2/' \
    net-dns/avahi/*.ebuild

# Drop all unmodified ebuilds, e.g. for older versions without qt4 USE flag
# For modified files, check number of changes
for i in net-dns/avahi/*.ebuild; do
    cmp -s "$i" "${PORTDIR:-/usr/portage}/$i" && rm $i
    nchg="$(diff -U0 "${PORTDIR:-/usr/portage}/$i" "$i" | grep ^+ | wc -l)"
    if [[ $nchg -ne 4 ]]; then
        diff -U0 "${PORTDIR:-/usr/portage}/$i" "$i" | grep ^+
        echo "Unexpected number of changes in $i: $nchg changes" >&2
        exit 1
    fi
done

# Redigest and show difference
ebuild $(ls -1 net-dns/avahi/*.ebuild | tail -n1) digest
Documentation/maintainers/generate_unmask

git add -A net-dns/avahi Documentation/package.unmask/kde-3.5
git status
echo 'Run: git commit -m "[net-dns/avahi] Update from main portage tree"'
